package com.china08.yxyapi.api.manage.sch.office.outernotice;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter.YxyNotifictionSchool;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeOutterRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 学校管理员 外部通知 已读列表查询
 * 
 */
@CrossOrigin
@RestController("SchOuterNoticeReceiveReadSrhController")
public class OuterNoticeReceiveReadSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeOutterRepository yxyNoticeOutterRepository;

    @RequestMapping(value = "/api_yw/manage/sch/office/outerNotice/receive/read/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        // 当前教育局id
        String edbId = getProfile(request, response).getSchoolId();
        // 当前用户的获取
        // String userId = getProfile(request, response).getId();
        // 未删除状态
        int isDelete = 0;
        List<YxyNoticeOutter> page = yxyNoticeOutterRepository
                .findByReceiveSchoolSchoolIdAndReceiveSchoolIsDeleteAndReceiveSchoolIsRead(edbId, isDelete, 1,
                        pageable);
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        RespModel respModel = null;
        Optional<YxyNotifictionSchool> yxyNotifictionSchool = null;
        for (YxyNoticeOutter yxyNoticeOutter : page) {
            respModel = new RespModel();
            respModel.setId(yxyNoticeOutter.getId());
            respModel.setTitle(yxyNoticeOutter.getTitle());
            respModel.setUserId(yxyNoticeOutter.getUserId());
            respModel.setUserNick(yxyNoticeOutter.getUserNick());
            respModel.setSchoolId(yxyNoticeOutter.getEdbId());
            respModel.setSchoolNick(yxyNoticeOutter.getEdbName() == null ? "未知教育局" : yxyNoticeOutter.getEdbName());
            respModel.createDate = DateToolUtils.format(yxyNoticeOutter.getCreatedDate(), "yyyy-MM-dd");
            // JAVA 8 判断查询接受用户信息
            yxyNotifictionSchool = yxyNoticeOutter.getCurrentReceiver(edbId);
            respModel.setIsRead(yxyNotifictionSchool.get().getIsRead());
            respModelLst.add(respModel);
        }
        Integer totalElements = yxyNoticeOutterRepository
                .countByReceiveSchoolSchoolIdAndReceiveSchoolIsDeleteAndReceiveSchoolIsRead(edbId, isDelete, 1);
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, totalElements);
        return respModelPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel {
        // 消息id
        private String id;
        // 消息标题
        private String title;
        // 发件人id
        private String userId;
        // 发件人名称
        private String userNick;
        // 发布对象 教育局id
        private String schoolId;
        // 发布对象 教育局名称
        private String schoolNick;
        // 是否已读 0:未读 1:已读
        private int isRead;

        public String createDate;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getUserNick() {
            return userNick;
        }

        public void setUserNick(String userNick) {
            this.userNick = userNick;
        }

        public String getSchoolId() {
            return schoolId;
        }

        public void setSchoolId(String schoolId) {
            this.schoolId = schoolId;
        }

        public String getSchoolNick() {
            return schoolNick;
        }

        public void setSchoolNick(String schoolNick) {
            this.schoolNick = schoolNick;
        }

        public int getIsRead() {
            return isRead;
        }

        public void setIsRead(int isRead) {
            this.isRead = isRead;
        }
    }
}